package com.atguigu03.threadsafe.exer;

/**
 * ClassName: PersonTest
 * Description:
 *
银行有一个账户。

有两个储户分别向同一个账户存3000元，每次存1000，存3次。每次存完打印账户余额。
问题：该程序是否有安全问题，如果有，如何解决？
 *
 *
 * @Author liu
 * @Create 2024/11/16 16:22
 * @Version 1.0
 */

class Account{
    private double balance;//余额
//重写方法synchoronized
    public synchronized void deposit(double amt) {
        if(amt >0){
            balance += amt;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
            System.out.println(Thread.currentThread().getName()+"存钱"+balance);

        }
    }


}
class Customer extends Thread{
 Account account;

    public Customer(Account account) {
        this.account = account;
    }
     public Customer(Account acct,String name){
        super(name);
        this.account = acct;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           account.deposit(1000);
        }

    }
}
public class Bank {
    public static void main(String[] args) {
        Account acct = new Account();
        Customer customer1 = new Customer(acct,"甲");
        Customer customer2 = new Customer(acct,"乙");

        customer1.start();
        customer2.start();


    }



}
